import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Test {

    public static void main(String[] args) {
        String s = "sss";

    }
}
class Solution {
    public int numSquares(int n) {
        int[] dp = new int[n+1];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0] = 0;
        for(int i=1; i*i<n+1; i++){
            for(int j=i*i; j<n+1; j++){
                dp[j] = Math.min(dp[j-i*i]+1,dp[j]);
            }
        }
        return dp[n];
    }
}
class Solution1 {
    public int captureForts(int[] forts) {
        int ans = 0;
        int prev = -1;
        for(int i=0; i<forts.length; i++){
            if(forts[i] == 1 || forts[i] == -1){
                if(prev>=0 && forts[i]*forts[prev]<0){
                    ans = Math.max(ans,i-prev-1);
                }
                prev = i;
            }
        }
        return ans;
    }
}
class Solution2 {
    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> set = new HashSet<>();
        for(String x : wordDict){
            set.add(x);
        }
        boolean[] dp = new boolean[s.length()+1];
        dp[0] = true;
        for(int j=0; j<s.length()+1; j++){
            for(int i=0; i<j; i++){
                String word = s.substring(i,j);
                if(set.contains(word) && dp[i]){
                    dp[j] = true;
                }
            }
        }
        return dp[s.length()];
    }
}
class Solution3 {
    /**
     dp[i][j]: 从j开始，走2^i所能到达的位置
     sum[i][j]: 从j开始，走2^i所能得到的和

     */
    public long getMaxFunctionValue(List<Integer> receiver, long K) {
        int n = receiver.size();
        int m = 64 - Long.numberOfLeadingZeros(K); //K的二进制长度
        int[][] dp = new int[m][n];
        long[][] sum = new long[m][n];
        for (int i = 0; i < n; i++) {//初始化
            dp[0][i] = receiver.get(i);
            sum[0][i] = receiver.get(i);
        }
        for (int i = 0; i < m - 1; i++) {
            for (int x = 0; x < n; x++) {
                dp[i+1][x] = dp[i][dp[i][x]];
                sum[i+1][x] = sum[i][x] + sum[i][dp[i][x]];//合并节点值之和
            }
        }

        long ans = 0;
        for (int i = 0; i < n; i++) {
            long s = i;
            int x = i;
            for (long k = K; k > 0; k &= k-1) {
                int ctz = Long.numberOfTrailingZeros(k);//从低到高最后一个0的位置相当于要走2^ctz
                s += sum[ctz][x];
                x = dp[ctz][x];
            }
            ans = Math.max(ans, s);
        }
        return ans;
    }
}